package com.fengniao.goods.service.impl;

import com.fengniao.goods.dao.CategoryMapper;
import com.fengniao.goods.pojo.Category;
import com.fengniao.goods.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.DBOperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * @author lanhai
 * @date 2021/7/5 17:01
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper CategoryMapper;

    /**
     * 条件查询代码抽取
     * @Categorym Category
     * @return
     */
    public Example getExample(Category category) {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();

        if (category != null) {
            if (!StringUtils.isEmpty(category.getId())) {
                criteria.andEqualTo("id", category.getId());
            }
            if (!StringUtils.isEmpty(category.getName())) {
                criteria.andLike("name", "%" + category.getName() + "%");
            }
            if (!StringUtils.isEmpty(category.getGoodsNum())) {
                criteria.andEqualTo("goods_num", category.getGoodsNum());
            }
            if (!StringUtils.isEmpty(category.getIsShow())) {
                criteria.andEqualTo("is_show", category.getIsShow());
            }
            if (!StringUtils.isEmpty(category.getIsMenu())) {
                criteria.andEqualTo("is_menu", category.getIsMenu());
            }
            if (!StringUtils.isEmpty(category.getSeq())) {
                criteria.andEqualTo("seq", category.getSeq());
            }
            if (!StringUtils.isEmpty(category.getParentId())) {
                criteria.andEqualTo("parent_id", category.getParentId());
            }
            if (!StringUtils.isEmpty(category.getTemplateId())) {
                criteria.andEqualTo("template_id", category.getTemplateId());
            }
        }
        return example;
    }


    @Override
    public PageInfo<Category> findPage(Category category, Integer num, Integer size) {

        PageHelper.startPage(num, size);
        List<Category> Categorys = findList(category);
        return new PageInfo<>(Categorys);
    }

    @Override
    public PageInfo<Category> findPage(Integer num, Integer size) {

        PageHelper.startPage(num, size);
        List<Category> Categorys = CategoryMapper.selectAll();
        return new PageInfo<>(Categorys);
    }

    @Override
    public List<Category> findList(Category category) {

        Example example = getExample(category);
        return CategoryMapper.selectByExample(example);
    }

    @Override
    public List<Category> findAll() {
        return CategoryMapper.selectAll();
    }

    @Override
    public Category findById(Integer id) {
        return CategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int add(Category category) {
        return CategoryMapper.insertSelective(category);
    }

    @Override
    public int revise(Category category) {
        return CategoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public int remove(Integer id) {
        return CategoryMapper.deleteByPrimaryKey(id);
    }


    @Override
    public List<Category> findByParentId(Integer pid) {
        Category category = new Category();
        category.setParentId(pid);
        List<Category> categories = CategoryMapper.select(category);
        DBOperUtil.ListIsNull(categories);
        return categories;
    }
}
